package com.sdk.utill;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

@Slf4j
public class HttpUtil {

   public static String sendPost(String url, String body, Map<String, String> handers) {
      CloseableHttpClient client = null;

      String var7;
      try {
         client = HttpClients.createDefault();
         HttpPost post = new HttpPost(url);
         post.setHeader("Content-Type", "application/json");
         if (handers != null) {
            Objects.requireNonNull(post);
            handers.forEach(post::setHeader);
         }

         StringEntity entity = new StringEntity(body);
         post.setEntity(entity);
         HttpResponse response = client.execute(post);
         if (response == null || response.getEntity() == null) {
            client.close();
            return null;
         }

         var7 = EntityUtils.toString(response.getEntity(), "UTF-8");
      } catch (Exception e) {
         log.error("发送请求时出错：", e);
         return null;
      } finally {
         try {
            if (client != null) {
            }

            client.close();
         } catch (IOException e) {
            log.error("关闭Http客户端时出现问题：", e);
         }

      }

      return var7;
   }

   public static String sendGet(String url, Map<String, String> headers) {
      try {
         CloseableHttpClient client = HttpClients.createDefault();

         String var5;
         label63: {
            try {
               try {
                  HttpGet get = new HttpGet(url);
                  if (headers != null) {
                     Objects.requireNonNull(get);
                     headers.forEach(get::setHeader);
                  }

                  HttpResponse response = client.execute(get);
                  if (response != null && response.getEntity() != null) {
                     var5 = EntityUtils.toString(response.getEntity(), "UTF-8");
                     break label63;
                  }

                  client.close();
               } catch (Exception e) {
                  log.error("发送GET请求时出错：", e);
               }
            } catch (Throwable var8) {
               if (client != null) {
                  try {
                     client.close();
                  } catch (Throwable var6) {
                     var8.addSuppressed(var6);
                  }
               }

               throw var8;
            }

            if (client != null) {
               client.close();
            }

            return null;
         }

         if (client != null) {
            client.close();
         }

         return var5;
      } catch (IOException e) {
         log.error("关闭Http客户端时出现问题：", e);
         return null;
      }
   }
}
